package com.example.product.config;

import com.example.product.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebConfig implements WebMvcConfigurer {
    @Value("${upload.filePath}")
    private String uploadPath;

    @Value("${upload.handler}")
    private String handler;
    /**
     * 项目跨域问题处理
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedOrigins("http://localhost:5173")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
    //创建拦截器Bean
    @Bean
    public MyInterceptor createInterceptorBean(){
        return new MyInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //addInterceptor(new MyInterceptor())配置拦截器
        //addPathPatterns("/**"):拦截url
        //excludePathPatterns():不拦截url swagger
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(handler)
                .excludePathPatterns("/files/upload")
                .excludePathPatterns("/files/download/{fileName}")
                .excludePathPatterns("/swagger-ui.html")
                .excludePathPatterns("/login")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/selectPage")
                .excludePathPatterns("/admin/home")
                .excludePathPatterns("/admin/add")
                .excludePathPatterns("/admin/update")
                .excludePathPatterns("/admin/delete/{id}")
                .excludePathPatterns("/admin/category")
                .excludePathPatterns("/admin/goods")
                .excludePathPatterns("/login/orders")
                .excludePathPatterns("/register")
                .excludePathPatterns("/notice/selectPage")
                .excludePathPatterns("/notice/add")
                .excludePathPatterns("/notice/update")
                .excludePathPatterns("/notice/delete/{id}")
                .excludePathPatterns("/notice/selectAll")
                .excludePathPatterns("/notice/selectById/{id}")
                .excludePathPatterns("/category/selectPage")
                .excludePathPatterns("/category/add")
                .excludePathPatterns("/category/update")
                .excludePathPatterns("/category/delete/{id}")
                .excludePathPatterns("/category/selectAll")
                .excludePathPatterns("/category/selectById/{id}")
                .excludePathPatterns("/goods/selectPage")
                .excludePathPatterns("/goods/add")
                .excludePathPatterns("/goods/update")
                .excludePathPatterns("/goods/delete/{id}")
                .excludePathPatterns("/goods/selectAll")
                .excludePathPatterns("/goods/selectById/{id}")
                .excludePathPatterns("/goodsStock/selectPage")
                .excludePathPatterns("/goodsStock/add")
                .excludePathPatterns("/goodsStock/update")
                .excludePathPatterns("/goodsStock/delete/{id}")
                .excludePathPatterns("/goodsStock/selectAll")
                .excludePathPatterns("/goodsStock/selectById/{id}")
                .excludePathPatterns("/user/selectPage")
               // .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/add")
                .excludePathPatterns("/user/update")
                .excludePathPatterns("/user/delete/{id}")
                .excludePathPatterns("/user/selectAll")
                .excludePathPatterns("/user/selectById/{id}")
                .excludePathPatterns("/orders/selectPage")
                .excludePathPatterns("/orders/add")
                .excludePathPatterns("/orders/update")
                .excludePathPatterns("/orders/delete/{id}")
                .excludePathPatterns("/orders/selectAll")
                .excludePathPatterns("/orders/selectById/{id}")
                .excludePathPatterns("/updatePassword")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
    }
    /**
     * swagger 静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler(handler)
                .addResourceLocations("file:" + uploadPath);
    }
}
